﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class Mail_Send : System.Web.UI.Page
    {

        SendRegisterBUS srBUS = null;
        SendRegisterDetailBUS srdBus = null;
        log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                try
                {
                    srBUS = new SendRegisterBUS();
                    loadDetailReport(true);
                }
                catch (Exception ex)
                {
                    pnError.Visible = true;
                    lblError.Text = ex.Message;
                    logs.Error(userLogin.Username + "-mailSend-load", ex);
                }

            }
            srBUS = new SendRegisterBUS();
        }
        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        protected void loadDetailReport(bool status)
        {
            srdBus = new SendRegisterDetailBUS();
            DataTable tblSendDetail = new DataTable();
            UserLoginDTO userLogin = getUserLogin();
            if (userLogin.DepartmentId == 1)
            {
                tblSendDetail = srdBus.GetByStatus(status);
            }
            else if (userLogin.DepartmentId == 2)
            {
                tblSendDetail = srdBus.GetByStatus_User(status, userLogin.UserId);
            }
            else tblSendDetail = srdBus.GetByStatus_SubUser(status, userLogin.UserId);
            if (tblSendDetail.Rows.Count > 0)
            {
                //dlReport.DataSource = tblSendDetail;
                //dlReport.DataBind();
                dlPager.MaxPages = 1000;
                dlPager.PageSize = 100;
                dlPager.DataSource = tblSendDetail.DefaultView;
                dlPager.BindToControl = dlReport;
                this.dlReport.DataSource = dlPager.DataSourcePaged;
                this.dlReport.DataBind();
                /* int count = 0;
                 for (int i = 0; i < tblSendDetail.Rows.Count; i++)
                 {
                     count++;
                     DataRow row = tblSendDetail.Rows[i];
                     //Label lblNo = (Label)dlReport.Items[i].FindControl("lblNo");
                     //lblNo.Text = count.ToString();

                     //HiddenField hdfId = (HiddenField)dlReport.Items[i].FindControl("hdfId");
                     //hdfId.Value = row["SendRegisterId"].ToString();

                     Label lblEmail = (Label)dlReport.Items[i].FindControl("lblEmail");
                     lblEmail.Text = row["Email"].ToString();

                     Label lblStartDate = (Label)dlReport.Items[i].FindControl("lblStartDate");
                     lblStartDate.Text = row["StartDate"].ToString();

                     Label lblEndDate = (Label)dlReport.Items[i].FindControl("lblEndDate");
                     lblEndDate.Text = row["EndDate"].ToString();

                     ImageButton ibtStatus = (ImageButton)dlReport.Items[i].FindControl("ibtStatus");
                     bool check = Boolean.Parse(row["Status"].ToString());
                     if (check == true)
                     {
                         ibtStatus.ImageUrl = "~/webapp/resource/images/ok.png";
                     }
                     else
                     {
                         ibtStatus.ImageUrl = "~/webapp/resource/images/warning.png";
                     }
                 }*/
            }
        }

        protected void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkAll = (CheckBox)sender;
            if (chkAll != null)
            {
                for (int i = 0; i < dlReport.Items.Count; i++)
                {
                    DataListItem item = dlReport.Items[i];
                    CheckBox chk = (CheckBox)item.FindControl("chkCheck");
                    chk.Checked = chkAll.Checked;
                }
            }
        }
        protected void lbtExecute_Click(object sender, EventArgs e)
        {
            try
            {
                DataListItem footer = (DataListItem)dlReport.Controls[dlReport.Controls.Count - 1];
                DropDownList drlType = (DropDownList)footer.FindControl("drlType");
                Label lblMessage = (Label)footer.FindControl("lblMessage");
                if (drlType.SelectedValue == "1")
                {
                    int countDelete = 0;
                    for (int i = 0; i < dlReport.Items.Count; i++)
                    {
                        DataListItem item = dlReport.Items[i];
                        HiddenField id = (HiddenField)item.FindControl("hdfId");
                        Label lblEmail = (Label)dlReport.Items[i].FindControl("lblEmail");

                        CheckBox chkXoa = (CheckBox)item.FindControl("chkCheck");
                        srdBus = new SendRegisterDetailBUS();
                        if (chkXoa.Checked == true)
                        {
                            ConnectionData.OpenMyConnection();
                            srdBus.tblSendRegisterDetail_Delete(int.Parse(id.Value), lblEmail.Text);
                            ConnectionData.OpenMyConnection();
                            countDelete++;
                        }
                    }

                }
                loadDetailReport(true);

            }
            catch (Exception ex)
            {
                //pnError.Visible = true;
                //lblError.Text = ex.Message;
            }
        }
        private int getSessionId()
        {
            if (Session["ID"].ToString() != null || Session["ID"].ToString() != "")
            {
                return int.Parse(Session["ID"].ToString());
            }
            return 0;
        }
        private int getSessionDepartmentId()
        {
            if (Session["DepartmentID"].ToString() != null || Session["DepartmentID"].ToString() != "")
            {
                return int.Parse(Session["DepartmentID"].ToString());
            }
            return 0;
        }
    }

}